﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BlueSkySOA.Service.SMSService.ApplicationService.Messaging;
using BlueSkySOA.Service.SMSService.ApplicationService.WebSendSms;
using BlueSkySOA.Service.SMSService.ApplicationService.Common;
using BlueSkySOA.Core.Configuration;
using BlueSkySOA.Core.ProxyServer;

namespace BlueSkySOA.Service.SMSService.ApplicationService.Impl
{
    public class SmsApplicationService: ISmsApplicationService
    {
        private IServerProxy _serverProxy;
        public SmsApplicationService(IServerProxy serverProxy)
        {
            _serverProxy = serverProxy;            
        }
        #region ISmsApplicationService Members

        public SendSmsResponse SendSms(Messaging.SendSmsRequest sendSmsRequest)
        {
            SendSmsResponse smsResponse = new SendSmsResponse() { ResultValue = false };
            if (((ServiceTypeEnum)sendSmsRequest.ServiceType) == ServiceTypeEnum.FastSending)
            {
                string serviceSignature = ApplicationSettingsFactory.GetApplicationSettings().SmsServiceSignarture;
                if (sendSmsRequest.Message.Length >= SmsServiceHelper.SmsLength - serviceSignature.Length)
                {
                    sendSmsRequest.Message = sendSmsRequest.Message.Substring(0, SmsServiceHelper.SmsLength - serviceSignature.Length);
                }
                sendSmsRequest.Message = string.Format("{0} {1}", sendSmsRequest.Message, serviceSignature);
            }
            ISendSmsProxy sendSmsProxy = SendSmsProxyFactory.CreateSendSmsProxyInstance(sendSmsRequest.ProxySmsKey);
            SendSmsPostData postData = sendSmsProxy.GeneratePostDataFor(sendSmsRequest);
            PostSmsDataResult webSendResult = sendSmsProxy.HandleCallback(sendSmsRequest, postData.PostDataAndValue, _serverProxy);
            smsResponse.ResultValue = webSendResult.Result;
            return smsResponse;
        }

        #endregion
    }
}
